<template>
  <div>
    <el-header class="header">
      <div class="logo">★ 店铺页面(hua.com)</div>
      <el-menu :default-active="activeIndex" class="top-nav-menu" mode="horizontal" @select="handleSelect">
        <el-menu-item index="1" @click="alertss">首页</el-menu-item>
        <el-menu-item index="6" class="cart-menu-item">
          <el-badge :value="cartCount" class="item" :max="99">
            <el-tooltip class="item" effect="dark" content="订单管理" placement="bottom">
              <i class="el-icon-shopping-cart-2" @click="alertsss"></i>
            </el-tooltip>
          </el-badge>
        </el-menu-item>
        <el-menu-item index="4" class="login-menu-item">
          <el-dropdown v-if="shopsName" @command="handleCommand">
            <span class="el-dropdown-link">
              你好, <span style="color: red">{{ shopsName }}</span>
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="personal">店铺详情</el-dropdown-item>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              <el-dropdown-item command="openUpdatePassword" :disabled="disableds">修改密码</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <template v-else>
            你好，请登录注册
          </template>
        </el-menu-item>
        <el-menu-item>
          <el-avatar :size="60" :src="src" shape="square">
            <img src="../imgs/error.png" alt="头像"/>
          </el-avatar>
        </el-menu-item>
      </el-menu>
    </el-header>
    <component :is="currentComponent" :key="componentKey"></component> <!-- 动态组件 -->
  </div>


</template>

<script>
import CommodityList from '@/components/CommodityList.vue'
import shopFirst from '@/components/ShopsFirst.vue'
import updatePasswords from "@/components/updatePasswords.vue";
import ordersLists from '@/components/ordersLists.vue'
import request from "@/utils/request";
import {removeToken} from "@/utils/auth";



export default {
  name: "shopsMain",
  data() {
    return {
      componentKey: 0,
      shopsName: '', // 存储从 cookie 中获取的用户名
      src: '', // 存储从 cookie 中获取的头像路径
      sbuinessForm: { // 定义用户表单数据
        sid: 0,
        sname: '',
        spassword: '',
        siphone: '',
        src: '',
      },
      sub:{
        sid: 0,
        sname: '',
        spassword: '',
        siphone: '',
        src: '',
      },
      shop: {
        sname: '',
        src: ''
      },
      isUserNameAvailable: false,
      currentComponent: shopFirst,
      activeIndex: '1',
      activeIndex2: '1',// 用于控制用户名是否可用
      disableds: false
    };
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    handleCommand(command) {
      if (command === 'logout') {
        this.shopsName = ''; // 清空用户名
        this.src = ''; // 清空头像地址
        removeToken();
        this.$router.replace('/');
        window.location.reload(true);
      } else if (command === 'personal') {
        this.currentComponent = CommodityList;
        this.disableds = true;
      }else if (command === 'openUpdatePassword') {
        this.componentKey += 1;
        this.currentComponent = updatePasswords
      }
    },
    getToken(){
      request({
        method: 'post',
        url: 'http://localhost:9092/shops/getTook',
      }).then(res => {
        if (res.data.code === 200) {
          console.log(res)
          this.sub = res.data.data;
          this.shopsName = res.data.data.sname
          this.shop.sname = this.shopsName;
          this.src = res.data.data.src
        }else {
          this.$router.replace("/")
        }
      })
    },
    alertss(){
      this.componentKey += 1;
      this.currentComponent = shopFirst;
      window.location.reload(true);
    },
    alertsss(){
      this.componentKey += 1;
      this.currentComponent = ordersLists;
    }

  },
  created() {
    this.getToken()
  },
};
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  color: #333;
  height: 60px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.logo {
  font-size: 20px;
  font-weight: bold;
  margin-left: 24px;
}

.top-nav-menu {
  display: flex;
  justify-content: center;
  width: 70%;
  align-items: center;
}

.cart-menu-item {
  position: relative;
  display: flex;
  align-items: center;
}

.item {
  margin: 0 10px;
  display: flex;
  align-items: center;
}

.el-icon-shopping-cart-2 {
  font-size: 20px;
}

.el-badge__content.is-fixed {
  position: absolute;
  top: 50%;
  transform: translateY(-50%) translateX(50%);
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  background-color: #f56c6c;
  color: #fff;
  text-align: center;
  line-height: 20px;
  padding: 0 5px;
  font-size: 12px;
  right: 0;
}
</style>